<template>
    <div class="app-container">
      <!--工具栏-->
      <div class="head-container">
        <eHeader :dict="dict"  />
        <crudOperation :permission="permission" />
      </div>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="name" label="名称、姓名" width="100"/>
        <el-table-column prop="phone" label="账号">
          <template slot-scope="scope">{{scope.row.phone}}</template>
        </el-table-column>
        <el-table-column prop="type" label="类型" width="100">
          <template slot-scope="scope">
            {{scope.row.type==1?'个人':'公司'}}
          </template>
        </el-table-column>
        <el-table-column prop="areaId" label="所在区域">
          <template slot-scope="scope">
            {{scope.row.province+'-'+scope.row.city+'-'+scope.row.district}}
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('createTime')" prop="createTime" label="创建日期">
          <template slot-scope="scope">
            <span>{{ scope.row.createTime }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="createrName" label="创建人"/>
        <el-table-column prop="enable" label="是否启用">
          <template slot-scope="scope">
            <el-switch @change="changeEnable(scope.row)"
                       v-model="scope.row.enable"
                       active-color="#13ce66"
                       inactive-color="#dcdfe6">
            </el-switch>
          </template>
        </el-table-column>
        <!--   编辑与删除   -->
        <el-table-column
          v-permission="['admin','job:edit','job:del']"
          label="操作"
          width="130px"
          align="center"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button class="filter-item" size="mini" type="primary" @click="permisManage(scope.row)">
              权限管理
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
      <!--表单组件-->
      <el-dialog append-to-body :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0"
                 :title="crud.status.title" width="800px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px" v-loading="formLoading">
          <el-form-item label="账号(手机号)" prop="phone">
            <el-input v-model="form.phone" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="代理商名称" prop="name">
            <el-input v-model="form.name" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="代理商类型" prop="type">
            <el-select v-model="form.type" placeholder="请选择">
              <el-option
                v-for="item in agentTypes"
                :key="item.value"
                :label="item.name"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="所在地区" prop="areaId">
            <el-select v-model="form.province" placeholder="请选择" @change="provinceChange">
              <el-option
                v-for="item in provinces"
                :key="item.id"
                :label="item.areaName"
                :value="item.id">
              </el-option>
            </el-select>
            <el-select v-model="form.city" placeholder="请选择" @change="cityChange">
              <el-option
                v-for="item in cities"
                :key="item.id"
                :label="item.areaName"
                :value="item.id">
              </el-option>
            </el-select>
            <el-select v-model="form.areaId" placeholder="请选择">
              <el-option
                v-for="item in districts"
                :key="item.id"
                :label="item.areaName"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="详细地址" prop="href">
            <el-input v-model="form.address" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <el-dialog append-to-body :close-on-click-modal="false" title="权限管理" width="800px" :visible.sync="permisDialog"
      @opened="permisDialogOpened">
        <uPermission ref="upermission" :agentID="currentAgent.id"></uPermission>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="permisDialog=false">取消</el-button>
          <el-button :loading="permisDialogLoading" type="primary" @click="permisConfirm">确认</el-button>
        </div>
      </el-dialog>
    </div>
</template>

<script>
    import CRUD, { presenter,form } from '@crud/crud'
    import eHeader from './module/header'
    import crudOperation from '@crud/CRUD.operation'
    import pagination from '@crud/Pagination'
    import udOperation from '@crud/UD.operation'
    import {getArea} from '@/api/manage/area'
    import crudAgent from '@/api/manage/agent'
    import uPermission from './module/userPermission'
    // crud交由presenter持有
    const crud = CRUD({
        title: '代理商',
        crudMethod: { ...crudAgent },
        url: 'api/manage/back/agent/list',
        sort: ['id,desc'],
        optShow: {
            add: true,
            edit: true,
            del: false,
            download: true
        }
    })
    const defaultForm={ name:'',type:1,phone:'' }
    export default {
        components:{
            eHeader,crudOperation,pagination,udOperation,uPermission
        },
        mixins: [presenter(crud),form(defaultForm)],
        dicts: ['job_status'],
        data() {
            return {
                permission: {
                    add: ['admin', 'job:add'],
                    edit: ['admin', 'job:edit'],
                    del: ['admin', 'job:del']
                },
                rules:{
                    phone:[{ required: true, message: '请输入账号（手机号）', trigger: 'blur' }],
                    name:[{ required: true, message: '请输入代理商名称', trigger: 'blur' }],
                    type:[{ required: true, message: '请选择代理商类型', trigger: 'blur' }],
                    areaId:[{ required: true, message: '请选择代理商所在区域', trigger: 'blur' }],
                },
                agentTypes:[
                    {value:1,name:"个人"},
                    {value:2,name:"公司"}
                ],
                formLoading:false,
                provinces:[],
                cities:[],
                districts:[],
                permisDialog:false,//授权弹框
                permisDialogLoading:false,
                currentAgent:{}
            }
        },
        methods:{
            // 新增与编辑前做的操作
            [CRUD.HOOK.afterToCU](crud, form) {
                if(this.provinces.length==0){
                    this.formLoading = true
                    //获取省份
                    getArea({provinceID:-1}).then(res=>{
                        this.provinces=res
                        this.formLoading = false
                    })
                }
                this.cities=[]
                this.districts=[]
            },
            provinceChange(val){
                this.formLoading = true
                //获取省份
                getArea({provinceID:val}).then(res=>{
                    this.cities=res
                    this.formLoading = false
                })
            },
            cityChange(val){
                this.formLoading = true
                //获取省份
                getArea({provinceID:val}).then(res=>{
                    this.districts=res
                    this.formLoading = false
                })
            },
            changeEnable(row){

            },
            permisManage(row){
                this.currentAgent=row
                this.permisDialog=true
            },
            permisDialogOpened(){
                this.$refs.upermission.init()
            },
            permisConfirm(){//权限弹框确认
                const _this=this
                this.$refs.upermission.savePermission(function () {
                    _this.permisDialog=false
                })
            }
        }
    }
</script>

<style scoped>

</style>
